public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<Resource>
HttpMessageConverter
that can read and write Resources
and supports byte range requests.
By default, this converter can read all media types. The Java Activation Framework (JAF) -
if available - is used to determine the Content-Type
of written resources.
If JAF is not available, application/octet-stream
is used.
This converter supports HTTP byte range requests and can write partial content, when provided
with an HttpRangeResource
instance containing the required Range information.
Modifier and Type | Class and Description |
---|---|
private static class |
ResourceHttpMessageConverter.ActivationMediaTypeFactory
Inner class to avoid a hard-coded JAF dependency.
|
Modifier and Type | Field and Description |
---|---|
private static boolean |
jafPresent |
logger
Constructor and Description |
---|
ResourceHttpMessageConverter() |
Modifier and Type | Method and Description |
---|---|
private void |
copyRange(java.io.InputStream in,
java.io.OutputStream out,
long start,
long end) |
protected java.lang.Long |
getContentLength(Resource resource,
MediaType contentType)
Returns the content length for the given type.
|
protected MediaType |
getDefaultContentType(Resource resource)
Returns the default content type for the given type.
|
private static void |
print(java.io.OutputStream os,
java.lang.String buf) |
private static void |
println(java.io.OutputStream os) |
protected Resource |
readInternal(java.lang.Class<? extends Resource> clazz,
HttpInputMessage inputMessage)
Abstract template method that reads the actual object.
|
protected boolean |
supports(java.lang.Class<?> clazz)
Indicates whether the given class is supported by this converter.
|
protected void |
writeContent(Resource resource,
HttpOutputMessage outputMessage) |
protected void |
writeInternal(Resource resource,
HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
|
protected void |
writePartialContent(HttpRangeResource resource,
HttpOutputMessage outputMessage)
Write parts of the resource as indicated by the request
Range header. |
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getDefaultCharset, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
protected boolean supports(java.lang.Class<?> clazz)
AbstractHttpMessageConverter
supports
in class AbstractHttpMessageConverter<Resource>
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected Resource readInternal(java.lang.Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws java.io.IOException, HttpMessageNotReadableException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.readInternal
in class AbstractHttpMessageConverter<Resource>
clazz
- the type of object to returninputMessage
- the HTTP input message to read fromjava.io.IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorsprotected MediaType getDefaultContentType(Resource resource)
AbstractHttpMessageConverter
AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
is invoked without a specified content type parameter.
By default, this returns the first element of the
supportedMediaTypes
property, if any.
Can be overridden in subclasses.
getDefaultContentType
in class AbstractHttpMessageConverter<Resource>
resource
- the type to return the content type fornull
if not knownprotected java.lang.Long getContentLength(Resource resource, MediaType contentType) throws java.io.IOException
AbstractHttpMessageConverter
By default, this returns null
, meaning that the content length is unknown.
Can be overridden in subclasses.
getContentLength
in class AbstractHttpMessageConverter<Resource>
resource
- the type to return the content length fornull
if not knownjava.io.IOException
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) throws java.io.IOException, HttpMessageNotWritableException
AbstractHttpMessageConverter
AbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.writeInternal
in class AbstractHttpMessageConverter<Resource>
resource
- the object to write to the output messageoutputMessage
- the HTTP output message to write tojava.io.IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errorsprotected void writeContent(Resource resource, HttpOutputMessage outputMessage) throws java.io.IOException, HttpMessageNotWritableException
java.io.IOException
HttpMessageNotWritableException
protected void writePartialContent(HttpRangeResource resource, HttpOutputMessage outputMessage) throws java.io.IOException
Range
header.resource
- the identified resource (never null
)outputMessage
- current servlet responsejava.io.IOException
- in case of errors while writing the contentprivate static void println(java.io.OutputStream os) throws java.io.IOException
java.io.IOException
private static void print(java.io.OutputStream os, java.lang.String buf) throws java.io.IOException
java.io.IOException
private void copyRange(java.io.InputStream in, java.io.OutputStream out, long start, long end) throws java.io.IOException
java.io.IOException